<?php

namespace Hookrock\Admin;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Route;

class AdminServiceProvider extends ServiceProvider
{
    protected array $commands = [
        Console\PublishCommand::class,
        Console\InstallsCommand::class,
    ];

    /**
     * 1、注册发布
     * 2、注册视图
     * 3、注册路由
     * 4、注册中间件
     * 5、注册翻译
     * 6、设置 auth 配置
     * @return void
     */
    public function boot(): void
    {
        $this->registerPublishing();
        $this->registerRoutes();
    }

    public function register(): void
    {
        $this->commands($this->commands);
    }

    /**
     * 注册发布
     * @return void
     */
    protected function registerPublishing(): void
    {
        if ($this->app->runningInConsole()) {
            $this->publishes([__DIR__ . '/../config' => config_path()], 'hookrock-admin-config');
            $this->publishes([__DIR__ . '/../resources/lang' => lang_path()], 'hookrock-admin-lang');
            $this->publishes([__DIR__ . '/../database/migrations' => database_path('migrations')], 'hookrock-admin-migrations');
        }
    }

    /**
     * 注册路由
     * @return void
     */
    protected function registerRoutes(): void
    {
        if (is_file($routes = admin_path('router.php'))) {
            $this->loadAdminRoutes($routes);
        }
    }

    /**
     * 加载后台路由
     *
     * @param $path
     * @return void
     */
    protected function loadAdminRoutes($path): void
    {
        Route::group(array_filter([]), $path);
    }
}
